﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cCreatProperty : System.Web.UI.UserControl
    {
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        CompanyBussiness.TypeProduct typeProduct;
        CompanyBussiness.TypeProperty typeProperty;
        CompanyBussiness.Property property;
        CompanyBussiness.Language language;
        CompanyBussiness.Product product;
        CheckValid cv = new CheckValid();
        int STRID;
        int STRtypeID;
        string error = "";
        bool AddOrUp = false;
        string modulCode;
        int idPropertyOut;

        protected void Page_Load(object sender, EventArgs e)
        {
            fail.Visible = false;
            success.Visible = false;
            if (Request[CONST.QUERYSTRING_PROPERTY] == null && Request[CONST.QUERYSTRING_TYPE_PROPERTY]!=null)
            {
                AddOrUp = false;
                STRtypeID = int.Parse(Request[CONST.QUERYSTRING_TYPE_PROPERTY]);
            }
            else if (Request[CONST.QUERYSTRING_PROPERTY] != null && Request[CONST.QUERYSTRING_TYPE_PROPERTY] == null)
            {
                AddOrUp = true;
                STRID = int.Parse(Request[CONST.QUERYSTRING_PROPERTY]);
                this.LoadForm();
               
            }
        }

        protected void LoadForm()
        {
            try
            {
                property = proMana.GetPropertyByID(STRID);
                txtPropertiesName.Text = property.PropertiesName;
                txtPropertiesDes.Value = property.PropertiesDes.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        protected bool GetData()
        {
            if (txtPropertiesDes.Value.ToString() != "")
            {
                property.PropertiesDes = txtPropertiesDes.Value;
            }
            else
            {
                fail.Visible = true;
                lbError.Text = "Bạn chưa nhập nội dung của thuộc tính !";
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (AddOrUp == false)
                {
                    #region Addnew

                    property = new Property();

                    if (GetData() == false)
                        return;

                    property.PropertiesName = txtPropertiesName.Text;

                    property.TypeProperties = STRtypeID;

                    int id=proMana.CreateProperty(property);


                    #endregion
                }
                else
                {
                    #region Update

                    property = proMana.GetPropertyByID(STRID);

                    if (GetData() == false)
                        return;

                    

                    proMana.UpdateProperty((int)property.PropertiesID,property.PropertiesName,property.PropertiesDes, (int)property.TypeProperties);

                    #endregion
                }
                }
            catch (Exception)
            {
                fail.Visible = true;
                lbError.Text = "Có lỗi trong khi sử lý dữ liệu . Bạn hãy kiểm tra lại.";
            }   
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtPropertiesDes.Value = "";
            txtPropertiesName.Text = "";
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.UrlReferrer.ToString());
        }
    }
}